//
//  B_PerfectUserMessageVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/12.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_PerfectUserMessageVC.h"
#import "XGPhotoManager.h"

@interface B_PerfectUserMessageVC ()

@property (nonatomic, strong) XGPhotoManager * photoManager;
@property (weak, nonatomic) IBOutlet UIButton *B_userImageButton;
- (IBAction)B_userImageAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *B_sexFilman;
@property (weak, nonatomic) IBOutlet UIButton *B_man;
- (IBAction)B_filmanAction:(id)sender;
- (IBAction)B_manAction:(id)sender;
@property (weak, nonatomic) IBOutlet BaseTextField *B_userNickName;

/// 头像
@property (nonatomic, strong) UIImage * image;
/// 性别  1 男  2  女
@property (nonatomic, assign) NSInteger sex;
@end

@implementation B_PerfectUserMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"完善信息"];
    self.B_userNickName.placeholder = @"请输入昵称";
    self.sex = 2;
    WeakSelf(weSelf, self);
    [self setRightTextBarButtonItemWithFrame:CGRectZero title:@"提交" titleColor:[UIColor color_text_white] backImage:@"" selectBackImage:@"" action:^(GUIButton *button) {
        [weSelf submitData];
    }];
    setUserHasInformation(@"2");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)B_userImageAction:(id)sender {
    WeakSelf(weSelf, self);
    [self alertViewWithTitle:@"请选择" message:@"" buttonsTitles:@[@"相机", @"从相册中选择", @"取消"] action:^(NSInteger index) {
        if (index == 0) {       // 相机
            
            [weSelf.photoManager openCamereWithController:weSelf withImage:^(UIImage *image) {
                [weSelf.B_userImageButton setBackgroundImage:image forState:UIControlStateNormal];
                weSelf.image = image;
            }];
        } else if (index == 1) {        // 相册
            [weSelf.photoManager openSinglePhotoWithController:weSelf withImage:^(UIImage *image) {
                [weSelf.B_userImageButton setBackgroundImage:image forState:UIControlStateNormal];
                weSelf.image = image;
            }];
        }
    }];
}
- (IBAction)B_filmanAction:(id)sender {
    self.sex = 2;
    self.B_sexFilman.backgroundColor = [UIColor color_pink];
    self.B_man.backgroundColor = [UIColor color_main_background_color];
}

- (IBAction)B_manAction:(id)sender {
    self.sex = 1;
    self.B_man.backgroundColor = [UIColor color_pink];
    self.B_sexFilman.backgroundColor = [UIColor color_main_background_color];
}

- (XGPhotoManager *)photoManager {
    if (!_photoManager) {
        _photoManager = [[XGPhotoManager alloc] init];
    }
    return _photoManager;
}

- (void) submitData {
    if (ISNULL(self.image)) {
        [KeyWindow makeToast:@"请选择头像"];
        return;
    }
    if (ISNULL(self.B_userNickName.text)) {
        [KeyWindow makeToast:@"请输入昵称"];
        return;
    }
    //TODO:执行提交
    
    // 在这个界面还设置了手势消失
    WeakSelf(weSelf, self);
    [NetworkingManager UploadmoreTaskWithURL:[NSString stringWithFormat:@"%@/yearnfor/inter/user/userPictureAdd", ServiceIp] imagesArray:@[self.image] parameterOfimages:@"files" parametersDict:nil compressionRatio:0.8 succeedBlock:^(NSDictionary *dict) {
        NSLog(@"dict  %@", dict);
        if ([dict[@"code"] isEqualToString:@"20010"]) {
            
            NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
            [dictionary setValue:dict[@"values"][0] forKey:@"portrait"];
            [dictionary setValue:getUserId() forKey:@"userId"];
            [dictionary setValue:weSelf.B_userNickName.text forKey:@"nickName"];
            [dictionary setValue:[NSString stringWithFormat:@"%ld", weSelf.sex] forKey:@"sex"];
            
            [NetworkingManager postNetworkRequestWithUrlString:@"/yearnfor/inter/user/updateUserInfo" parameters:@{@"str": [dictionary mj_JSONString]} isCache:NO succeed:^(id data) {
                if ([data[@"code"] isEqualToString:@"20006"]) {
                    setUserHasInformation(@"1");
                    [weSelf.navigationController.viewControllers.firstObject dismissViewControllerAnimated:YES completion:nil];
                    
                } else {
                    [KeyWindow makeToast:data[@"message"]];
                }
            } fail:^(NSError *error) {
                [KeyWindow makeToast:@"服务器异常"];
            }];
            
        }
    } failedBlock:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
    
    
    
    
    
    
    
}

@end
